package com.csun.cmny.security.core.authentication;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.Collection;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-10-22 17:37
 * @Description:
 */
public class HuaLongAuthenticationToken extends AbstractAuthenticationToken {

    private final Object principal;
    private Object credentials;
    private Object role;

    HuaLongAuthenticationToken(Object principal, Object credentials, Object role) {
        super(null);
        this.principal = principal;
        this.credentials = credentials;
        this.role = role;
        setAuthenticated(false);
    }

    HuaLongAuthenticationToken(Object principal, Object credentials, Object role,
                               Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.role = role;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public Object getRole() { return this.role; }

    @Override
    public void setAuthenticated(boolean isAuthenticated) {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
        credentials = null;
    }


}
